<?php

namespace eu\freeplace\page\model;

use eu\freeplace\page\model\ValueObject as ValueObject;

class MetaVO extends ValueObject {

    const CLASS_NAME = "MetaVO";
    const DB_TABLE   = "meta";

    private $ContentId   = null;
    private $Title       = null;
    private $Description = null;
    private $Keywords    = null;
    private $Domain      = null;
    private $Locale      = null;
    private $SearchBot   = null;

    public function __construct($contentID) {
        global $logger;
        $logger->log("Calling instance of ".self::CLASS_NAME."( ".$contentID." )", "NOTICE");

        try {
            if (strlen($contentID) < 1)
                throw new Exception(self::CLASS_NAME . " - could not fetch Meat Information Document[ID:".$contentID."].");

            $this->ContentId = $contentID;
        } catch (Exception $e) {

            $logger->log($e->getMessage(), "NOTICE");
        }
        return null;
    }

    public function __clone() {
        self::fetchMeta($this->ContentId);
    }

    public function __toString() {
        return "{/**TODO: implement me */}";
    }

    protected function createDatabaseTable() {
        $sql =
        "CREATE TABLE IF NOT EXISTS ".$this->getTablePrefix().self::DB_TABLE." (
            ContentId       BIGINT NOT NULL,
            Title           char(80),
            Description     char(180),
            Keywords        CHAR(255),
            Domain          char(100) NOT NULL,
            Locale          char(10) default 'de-DE',
            SearchBot       char(20) NOT NULL DEFAULT 'index,follow',

            PRIMARY KEY(ContentId)
        );";
        return $sql;
    }

    public function getClassAttributes() {
        $meta['ContentId']   = $this->ContentId;
        $meta['Title']       = $this->Title;
        $meta['Description'] = $this->Description;
        $meta['Keywords']    = $this->Keywords;
        $meta['Domain']      = $this->Domain;
        $meta['Locale']      = $this->Locale;
        $meta['SearchBot']   = $this->SearchBot;
        return $meta;
    }

    private function fetchMeta($contentID) {
        global $logger;

        $table_name = $this->getTablePrefix().self::DB_TABLE;
        $sql = "SELECT * FROM ".$table_name." WHERE ContentId=".$contentID;

        $logger->log(self::CLASS_NAME."().fetchMeta()", "DEBUG");
        $logger->log("\t".$sql, "DEBUG");

        $pdo = $this->getResult($sql);
        $result = $pdo->fetch();

        if($result != 0) {
            $this->ContentId   = $result['DocumentType'];
            $this->Title       = $result['Title'];
            $this->Description = $result['Description'];
            $this->Keywords    = $result['Keywords'];
            $this->Domain      = $result['Domain'];
            $this->Locale      = $result['Locale'];
            $this->SearchBot   = $result['SearchBot'];
        } else {
            $logger->log("\tCould not fetch Meta Tag's.", "DEBUG");
        }
    }

    ### [TODO]##################################################################



}//CLASS

?>
